﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Data;
using FineUI;


public class BaseTree : System.IDisposable
{
    public BaseTree()
    {

    }
    
    #region  创建tree
    /// <summary>
    /// 创建tree
    /// </summary>
    /// <param name="tree">tree控件ID</param>
    /// <param name="ds">数据集</param>
    /// <param name="nodeText">node显示的字段</param>
    /// <param name="nodeIcon">node图标字段</param>
    /// <param name="nodePid">node父id字段</param>
    /// <param name="navigateUrl">链接地址</param>
    public static void CreateTree(FineUI.Tree tree, DataSet ds, string nodeText, string nodeIcon,string nodePid, string navigateUrl)
    {
        CreateTree(tree, ds, nodeText, nodeIcon, nodePid, navigateUrl, true, false, false, false);
    }

    /// <summary>
    /// 创建tree
    /// </summary>
    /// <param name="tree">tree控件ID</param>
    /// <param name="ds">数据集</param>
    /// <param name="nodeText">node显示的字段</param>
    /// <param name="nodeIcon">node图标字段</param>
    /// <param name="nodePid">node父id字段</param>
    /// <param name="navigateUrl">链接地址</param>
    /// <param name="allowShowColumn">控制是否显示节点的字段名称</param>
    public static void CreateTree(FineUI.Tree tree, DataSet ds, string nodeText, string nodeIcon, string nodePid, string navigateUrl, string allowShowColumn)
    {
        CreateTree(tree, ds, nodeText, nodeIcon, nodePid, navigateUrl, true, false, false, false, allowShowColumn);
    }

    /// <summary>
    /// 创建tree
    /// </summary>
    /// <param name="tree">tree控件ID</param>
    /// <param name="ds">数据集</param>
    /// <param name="nodeText">node显示的字段</param>
    /// <param name="nodeIcon">node图标字段</param>
    /// <param name="nodePid">node父id字段</param>
    /// <param name="expanded">是否展开节点</param>
    /// <param name="enableClickEvent">是否单击节点回发</param>
    /// <param name="enableCheckBox">是否采用复选框</param>
    /// <param name="enableCheckEvent">是否启用复选框回发事件</param>
    /// <param name="navigateUrl">链接地址</param>
    public static void CreateTree(FineUI.Tree tree, DataSet ds, string nodeText, string nodeIcon, string nodePid,
        string navigateUrl, bool expanded, bool enableClickEvent, bool enableCheckBox, bool enableCheckEvent)
    {
        CreateTree(tree, ds, nodeText, nodeIcon, nodePid, navigateUrl, expanded, enableClickEvent, enableCheckBox, enableCheckEvent, string.Empty);
    }

    /// <summary>
    /// 创建tree
    /// </summary>
    /// <param name="tree">tree控件ID</param>
    /// <param name="ds">数据集</param>
    /// <param name="nodeText">node显示的字段</param>
    /// <param name="nodeIcon">node图标字段</param>
    /// <param name="nodePid">node父id字段</param>
    /// <param name="expanded">是否展开节点</param>
    /// <param name="enableClickEvent">是否单击节点回发</param>
    /// <param name="enableCheckBox">是否采用复选框</param>
    /// <param name="enableCheckEvent">是否启用复选框回发事件</param>
    /// <param name="navigateUrl">链接地址</param>
    /// <param name="allowShowColumn">控制是否显示节点的字段名称</param>
    public static void CreateTree(FineUI.Tree tree, DataSet ds, string nodeText, string nodeIcon, string nodePid, string navigateUrl, bool expanded, bool enableClickEvent, bool enableCheckBox, bool enableCheckEvent, string allowShowColumn)
    {
        string showStr = string.Empty;
        if (allowShowColumn.Length > 0)
        {
            showStr = " and " + allowShowColumn + ">0 ";
        }

        DataRow[] drParent = ds.Tables[0].Select(nodePid + "=0 " + showStr);

        TreeNode node;

        foreach (DataRow dr in drParent)
        {
            node = addTreeNode(dr, nodeText, nodeIcon,expanded, enableClickEvent, enableCheckBox, enableCheckEvent, navigateUrl);

            tree.Nodes.Add(node);

            ResolveSubTree(ds, dr["id"].ToString(), node, nodeText, nodeIcon, nodePid, expanded, enableClickEvent, enableCheckBox, enableCheckEvent, navigateUrl, allowShowColumn);
        }
    }
    

    /// <summary>
    /// 循环子节点
    /// </summary>
    /// <param name="ds">数据集</param>
    /// <param name="parentId">父编号</param>
    /// <param name="nodeText">node显示的字段</param>
    /// <param name="nodeIcon">node图标字段</param>
    /// <param name="nodePid">node父id字段</param>
    /// <param name="expanded">是否展开节点</param>
    /// <param name="enableClickEvent">是否点击节点回发</param>
    /// <param name="enableCheckBox">是否采用复选框</param>
    /// <param name="enableCheckEvent">是否启用复选框回发事件</param>
    /// <param name="navigateUrl">链接地址</param>
    ///  <param name="allowShowColumn">控制是否显示节点的字段名称</param>
    private static void ResolveSubTree(DataSet ds, string parentId, TreeNode treeNode, string nodeText, string nodeIcon, string nodePid, bool expanded, bool enableClickEvent, bool enableCheckBox, bool enableCheckEvent, string navigateUrl, string allowShowColumn)
    {
        string showStr = string.Empty;
        if (allowShowColumn.Length > 0)
        {
            showStr = " and " + allowShowColumn + ">0 ";
        }
        DataRow[] drParent = ds.Tables[0].Select(nodePid + "=" + parentId + " " + showStr);

        TreeNode node;
        foreach (DataRow dr in drParent)
        {
            node = addTreeNode(dr, nodeText, nodeIcon,expanded, enableClickEvent, enableCheckBox, enableCheckEvent,navigateUrl);
            treeNode.Nodes.Add(node);
            ResolveSubTree(ds, dr["id"].ToString(), node, nodeText, nodeIcon, nodePid, expanded, enableClickEvent, enableCheckBox, enableCheckEvent, navigateUrl, allowShowColumn);
        }
    }

    private static TreeNode addTreeNode(DataRow dr,  string nodeText, string nodeIcon,bool expanded, bool enableClickEvent, bool enableCheckBox, bool enableCheckEvent, string navigateUrl)
    {
        TreeNode node = new TreeNode();
        node.NodeID = dr["id"].ToString();
        node.Text = dr[nodeText].ToString();

        if (dr[nodeIcon].ToString().Length > 0)
            node.IconUrl = "/" + dr[nodeIcon].ToString();

        node.EnableClickEvent = enableClickEvent;
        node.EnableCheckBox = enableCheckBox;
        node.EnableCheckEvent = enableCheckEvent;
        node.Expanded = expanded;

        string tempurl = dr[navigateUrl].ToString();
        if (!string.IsNullOrEmpty(tempurl))
            node.NavigateUrl = tempurl;
        return node;
    }

    #endregion

    #region  创建accordion

    /// <summary>
    /// 创建accordion
    /// </summary>
    /// <param name="accordion"></param>
    /// <param name="ds"></param>
    /// <param name="title"></param>
    /// <param name="iconUrl"></param>
    /// <param name="pidcolumn"></param>
    /// <param name="navigateUrl"></param>
    public static void CreateAccordion(Accordion accordion, DataSet ds, string title, string iconUrl, string pidcolumn,
        string navigateUrl)
    {
        CreateAccordion(accordion, ds, title, iconUrl, pidcolumn, navigateUrl,string.Empty);
    }

    /// <summary>
    /// 创建accordion
    /// </summary>
    /// <param name="accordion"></param>
    /// <param name="ds"></param>
    /// <param name="title"></param>
    /// <param name="iconUrl"></param>
    /// <param name="pidcolumn"></param>
    /// <param name="navigateUrl"></param>
    ///  <param name="allowShowColumn">控制是否显示节点的字段名称</param>
    public static void CreateAccordion(Accordion accordion, DataSet ds, string title, string iconUrl, string pidcolumn, string navigateUrl, string allowShowColumn)
    {
        string showStr = string.Empty;
        if (allowShowColumn.Length > 0)
        {
            showStr = " and " + allowShowColumn + ">0 ";
        }
        DataRow[] drParent = ds.Tables[0].Select(pidcolumn + "=0 " + showStr);

        foreach (DataRow dr in drParent)
        {
            AccordionPane pane = new AccordionPane();
            accordion.Panes.Add(pane);
            pane.Title = dr[title].ToString();
            pane.IconUrl = dr[iconUrl].ToString();
            pane.ShowBorder = false;

            //创建该节点下的菜单目录,并将该手风琴节点的ID作为下属菜单的父节点                
            ResolveSubTree(ds, dr["ID"].ToString(), title, iconUrl, pidcolumn, navigateUrl, pane, allowShowColumn);
        }
    }

    /// <summary>
    /// 循环子节点
    /// </summary>
    /// <param name="ds">数据集</param>
    /// <param name="parentId">父编号</param>
    /// <param name="nodeText">node显示的字段</param>
    /// <param name="nodeIcon">node图标字段</param>
    /// <param name="nodePid">node父id字段</param>
    /// <param name="navigateUrl">链接地址</param>
    /// <param name="pane">要加载到Accordion</param>
    ///  <param name="allowShowColumn">控制是否显示节点的字段名称</param>
    private static void ResolveSubTree(DataSet ds, string parentId, string nodeText, string nodeIcon,string nodePid, string navigateUrl, AccordionPane pane,string allowShowColumn)
    {
        string showStr = string.Empty;
        if (allowShowColumn.Length > 0)
        {
            showStr = " and " + allowShowColumn + ">0 ";
        }
        DataRow[] drParent = ds.Tables[0].Select(nodePid + "=" + parentId + " " + showStr);

        FineUI.Tree tree = new FineUI.Tree();
        pane.Items.Add(tree);
        tree.ShowBorder = false;
        tree.ShowHeader = false;
        tree.AutoScroll = false;
        tree.EnableArrows = true;

        TreeNode TN;
        foreach (DataRow dr in drParent)
        {
            TN = new TreeNode();
            TN.NodeID = dr["id"].ToString();
            TN.Text = dr[nodeText].ToString();
            TN.IconUrl = dr[nodeIcon].ToString();
            if (!string.IsNullOrEmpty(navigateUrl))
                TN.NavigateUrl = dr[navigateUrl].ToString();
            tree.Nodes.Add(TN);

            ResolveSubTree(ds, dr["ID"].ToString(), TN, nodeText, nodeIcon, nodePid, true, false, false, false, navigateUrl, allowShowColumn);
        }
    }
    #endregion

    #region 循环展开节点(用try接此方法)
    /// <summary>
    /// 递归调用父节点，并使节点展开(注意会有找不到父节点的异常)
    /// </summary>
    /// <param name="node"></param>
    public static void SetTreeExpanded(TreeNode node)
    {
        TreeNode newNode = node.ParentNode;
        newNode.Expanded = true;
        SetTreeExpanded(newNode);
    }
    #endregion

    #region 重新设置每个节点的图标
    /// <summary>
    /// 重新设置每个节点的图标
    /// </summary>
    /// <param name="nodes"></param>
    public static void ResolveTreeNode(TreeNodeCollection nodes)
    {
        foreach (TreeNode node in nodes)
        {
            if (node.Nodes.Count == 0)
            {
                if (!String.IsNullOrEmpty(node.NavigateUrl))
                {
                    node.IconUrl = GetIconForTreeNode(node.NavigateUrl);
                }
            }
            else
            {
                ResolveTreeNode(node.Nodes);
            }
        }
    }
    #endregion

    #region 根据链接地址返回相应的图标
    /// <summary>
    /// 根据链接地址返回相应的图标
    /// </summary>
    /// <param name="url"></param>
    /// <returns></returns>
    public static string GetIconForTreeNode(string url)
    {
        string iconUrl = "~/res/images/filetype/vs_unknow.png";
        url = url.ToLower();
        int lastDotIndex = url.LastIndexOf('.');
        string fileType = url.Substring(lastDotIndex + 1);

        switch (fileType)
        {
            case "txt":
                iconUrl = "~/res/images/filetype/vs_txt.png";
                break;
            case "aspx":
                iconUrl = "~/res/images/filetype/vs_aspx.png";
                break;
            case "htm":
            //break;
            case "html":
                iconUrl = "~/res/images/filetype/vs_htm.png";
                break;
        }

        return iconUrl;
    }
    #endregion

    #region 实例化方法
    public BaseTree(FineUI.Tree treeCtrl)
    {
        _tree = treeCtrl;
    }

    /// <summary>
    /// 创建tree
    /// </summary>
    public void CreateTree()
    {

        DataRow[] drParent = TreeDataSet.Tables[0].Select(ColumnNodePid + "=0");

        TreeNode node;

        foreach (DataRow dr in drParent)
        {
            node = AddTreeNode(dr);

            _tree.Nodes.Add(node);

            ResolveSubTree(dr["id"].ToString(), node);
        }
    }

    public void Dispose()
    {
        //释放数据源资源
        TreeDataSet.Dispose();
    }

    /// <summary>
    /// 循环子节点
    /// </summary>
    private void ResolveSubTree(string parentId, TreeNode treeNode)
    {
        DataRow[] drParent = TreeDataSet.Tables[0].Select(ColumnNodePid + "=" + parentId + "");

        TreeNode node;
        foreach (DataRow dr in drParent)
        {
            node = AddTreeNode(dr);
            treeNode.Nodes.Add(node);
            ResolveSubTree(dr["id"].ToString(), node);
        }
    }

    private TreeNode AddTreeNode(DataRow dr)
    {
        TreeNode node = new TreeNode();
        node.NodeID = dr[ColumnNodeId].ToString();
        node.Text = dr[ColumnNodeText].ToString();

        if (dr[ColumnNodeIcon].ToString().Length > 0)
            node.IconUrl = "/" + dr[ColumnNodeIcon].ToString();

        node.EnableClickEvent = EnableClickEvent;
        node.EnableCheckBox = EnableCheckBox;
        node.EnableCheckEvent = EnableCheckEvent;
        node.Expanded = Expanded;

        if (EnableCheckBox)
        {
            //判断node.NodeID是否在checkValue中
            if ((CheckValue).Contains(node.NodeID))
            {
                node.Checked = true;
            }
            else
            {
                node.Checked = Checked;
            }
        }

        node.EnableCollapseEvent = EnableCollapseEvent;
        node.EnableExpandEvent = EnableExpandEvent;

        string tempurl = dr[ColumnNavigateUrl].ToString();
        if (!string.IsNullOrEmpty(tempurl))
            node.NavigateUrl = tempurl;

        return node;
    }

    #region 属性

    private string _columnNodeText;
    private string _columnNodeId;
    private string _columnNodeIcon;
    private string _columnNodePid;
    private string _columnNavigateUrl;

    private bool _expanded=true;
    private bool _checked=false;

    private bool _enableCheckBox=false;
    private bool _enableCheckEvent=false;

    private bool _enableClickEvent=false;
    private bool _enableCollapseEvent=false;
    private bool _enableExpandEvent=false;

    private FineUI.Tree _tree;
    private IList _checkValue;
    private DataSet _dataSet;

    /// <summary>
    /// 显示tree文本的字段
    /// </summary>
    public string ColumnNodeText
    {
        get { return _columnNodeText; }
        set { _columnNodeText = value; }
    }

    /// <summary>
    /// 关联tree id的字段
    /// </summary>
    public string ColumnNodeId
    {
        get { return _columnNodeId; }
        set { _columnNodeId = value; }
    }

    /// <summary>
    /// 关联tree 图标的字段
    /// </summary>
    public string ColumnNodeIcon
    {
        get { return _columnNodeIcon; }
        set { _columnNodeIcon = value; }
    }

    /// <summary>
    /// 关联tree 父id的字段
    /// </summary>
    public string ColumnNodePid
    {
        get { return _columnNodePid; }
        set { _columnNodePid = value; }
    }

    /// <summary>
    /// 关联tree 链接的字段
    /// </summary>
    public string ColumnNavigateUrl
    {
        get { return _columnNavigateUrl; }
        set { _columnNavigateUrl = value; }
    }

    /// <summary>
    /// treenode 是否展开
    /// </summary>
    public bool Expanded
    {
        get { return _expanded; }
        set { _expanded = value; }
    }

    /// <summary>
    /// treenode 是否选中
    /// </summary>
    public bool Checked
    {
        get { return _checked; }
        set { _checked = value; }
    }

    /// <summary>
    /// treenode 是否有多选框
    /// </summary>
    public bool EnableCheckBox
    {
        get { return _enableCheckBox; }
        set { _enableCheckBox = value; }
    }

    /// <summary>
    /// treenode 是否有多选事件
    /// </summary>
    public bool EnableCheckEvent
    {
        get { return _enableCheckEvent; }
        set { _enableCheckEvent = value; }
    }


    /// <summary>
    /// treenode 是否有点击事件
    /// </summary>
    public bool EnableClickEvent
    {
        get { return _enableClickEvent; }
        set { _enableClickEvent = value; }
    }

    /// <summary>
    /// treenode 是否折叠事件
    /// </summary>
    public bool EnableCollapseEvent
    {
        get { return _enableCollapseEvent; }
        set { _enableCollapseEvent = value; }
    }

    /// <summary>
    /// treenode 是否展开事件
    /// </summary>
    public bool EnableExpandEvent
    {
        get { return _enableExpandEvent; }
        set { _enableExpandEvent = value; }
    }


    /// <summary>
    /// 选中值
    /// </summary>
    public IList CheckValue
    {
        get { return _checkValue; }
        set { _checkValue = value; }
    }

    /// <summary>
    /// 绑定数据源
    /// </summary>
    public DataSet TreeDataSet
    {
        get { return _dataSet; }
        set { _dataSet = value; }
    }

    #endregion

  

    
    #endregion

  

}

